import json
import os
import shutil

from CommonOperations.get_all_configs import get_local_config
from CommonOperations.parse_zip import create_zip_file
from CommonOperations.project_root_path import project_root_path
from CommonOperations.parse_img import create_img


def get_json_data(file_path):
    """
    从对应路径读取json文件数据
    :param file_path: json文件路径
    :return:image_setting 图片配置项
            font_setting 文字配置项
            time_setting 所有图片列表
            image_list 图片列表
            dir_setting 文件夹配置项
    """
    with open(file=file_path, mode='r') as f_p:
        origin_json = json.load(f_p)
    image_setting = origin_json['Image_setting']
    font_setting = origin_json['Font_setting']
    time_setting = origin_json['Time_setting']
    image_list = origin_json['Image_list']
    dir_setting = origin_json['Dir_setting']

    return image_setting, font_setting, time_setting, image_list, dir_setting

def batch_create_imgs_from_json(file_path):
    """
    批量生成图片，批量生成文件夹为BatchImages
    :param file_path: 配置文件路径
    :return: 批量文件夹路径
    """
    # 获取json文件中的配置信息
    image_setting, font_setting, time_setting, image_list, dir_setting = get_json_data(file_path)

    # 初始化批量导出文件夹
    batch_imgs_root_dir = os.path.join(project_root_path(), "BatchImages")
    if not os.path.exists(batch_imgs_root_dir):
        os.mkdir(batch_imgs_root_dir)
    batch_imgs_dir = os.path.join(batch_imgs_root_dir, dir_setting['dir_name'])
    if not os.path.exists(batch_imgs_dir):
        os.mkdir(batch_imgs_dir)
    # 清空文件夹
    shutil.rmtree(batch_imgs_dir)

    # 获取配置信息
    for img in image_list:
        if image_setting['default_jpg'] is True:
            create_img(img_width=img['width'] if img['width'] > 0 else image_setting['default_width'],
                       img_height=img['height'] if img['height'] > 0 else image_setting['default_height'],
                       whether_add_text=font_setting['whether_add_text'],
                       font_color=font_setting['default_color'],
                       font_size=font_setting['default_size'],
                       desc_text=img['name'],
                       whether_add_timestamp=time_setting['whether_timestamp'],
                       img_type="JPG",
                       img_dir=batch_imgs_dir,
                       whether_add_border=image_setting['whether_add_border'],
                       border_color=image_setting['border_color'],
                       border_width=image_setting['border_width'])
        if image_setting['default_png'] is True:
            create_img(img_width=img['width'] if img['width'] > 0 else image_setting['default_width'],
                       img_height=img['height'] if img['height'] > 0 else image_setting['default_height'],
                       whether_add_text=font_setting['whether_add_text'],
                       font_color=font_setting['default_color'],
                       font_size=font_setting['default_size'],
                       desc_text=img['name'],
                       whether_add_timestamp=time_setting['whether_timestamp'],
                       img_type="PNG",
                       img_dir=batch_imgs_dir,
                       whether_add_border=image_setting['whether_add_border'],
                       border_color=image_setting['border_color'],
                       border_width=image_setting['border_width'])
        if image_setting['default_gif'] is True:
            create_img(img_width=img['width'] if img['width'] > 0 else image_setting['default_width'],
                       img_height=img['height'] if img['height'] > 0 else image_setting['default_height'],
                       whether_add_text=font_setting['whether_add_text'],
                       font_color=font_setting['default_color'],
                       font_size=font_setting['default_size'],
                       desc_text=img['name'],
                       whether_add_timestamp=time_setting['whether_timestamp'],
                       img_type="GIF",
                       img_dir=batch_imgs_dir,
                       whether_add_border=image_setting['whether_add_border'],
                       border_color=image_setting['border_color'],
                       border_width=image_setting['border_width'])

    if dir_setting['whether_zip'] is True:
        zip_file = create_zip_file(batch_imgs_dir)
        return zip_file

    return batch_imgs_dir

def create_json_demo_file():
    """
    创建json模板文件
    :return: 模板文件路径
    """
    json_demo = get_local_config()
    img_list = []
    img_list.append({"name": "图片1", "width": 100, "height": 100})
    img_list.append({"name": "图片2", "width": 200, "height": 100})
    json_demo['Dir_setting'] = {"dir_name": "批量文件夹json", "whether_zip": False}
    json_demo['Image_list'] = img_list

    json_demo_string = json.dumps(json_demo, indent=2, ensure_ascii=False, separators=(",", ":"))

    json_demo_path = os.path.join(project_root_path(), "TemplateDemos", "Demo.json")
    demo_dir = os.path.join(project_root_path(), "TemplateDemos")
    if not os.path.exists(demo_dir):
        os.mkdir(demo_dir)
    if os.path.exists(json_demo_path):
        os.remove(json_demo_path)
    with open(json_demo_path, 'w') as j_d:
        j_d.write(json_demo_string)

    return json_demo_path
